var ExpressConstructor = require('express');
var app = ExpressConstructor();
var fsd = require('./fstable');
var fd = new fsd.FSTable("./data", ["firstName", "lastName"], "id");

function RESTModule(port) {
    this.port = port;
    this.path = "/rest";

    app.use(function (req, res, next) {
	res.setHeader('Access-Control-Allow-Origin', '*');
	res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
	res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
	res.setHeader('Access-Control-Allow-Credentials', false);
	next();
    });

    app.use(ExpressConstructor.bodyParser());

    app.use("/", ExpressConstructor.static(__dirname));

    app.get(this.path + '/wines', function(req, res) {
        console.log("REST Call!");
        res.send([{name: 'wine1'}, {name: 'wine2'}]);
    });

    app.get(this.path + '/wines/:id', function(req, res) {
        res.send({id: req.params.id, name: "The Name", description: "description"});
    });

    app.get(this.path + '/time', function(req, res) {
        var date = new Date();
        var dateString = date.toUTCString();
        console.log("REST Time Call: " + dateString);
        res.send({time: dateString});
    });

    app.get(this.path + '/persons', function(req, res) {
        console.log("REST Person list");
        var responseData = [
            {firstName: "John", lastName: "Doe"},
            {firstName: "Anna", lastName: "Smith"},
            {firstName: "Peter", lastName: "Jones"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Xxx", lastName: "Yyy"},
            {firstName: "Imre", lastName: "Tabur"}
        ];
        res.send(responseData);
    });


    app.get(this.path + '/persons/:id', function(req, res) {
        console.log("REST Person call: " + req.params.id);
        var responseData = {firstName: "Imre", lastName: "Tabur"};
        res.send(responseData);
    });

    app.post(this.path + '/persons', function(req, res) {
        console.log("REST Person POST call: " + req.body);
        var record = fd.newRecord();
        record.firstName = req.body.firstName;
        record.lastName = req.body.lastName;
        var cleanObj = record.save();
        res.send(cleanObj);
    });

    app.delete(this.path + '/persons', function(req, res) {
        console.log("REST Person DELETE call: " + req.body);
        res.send(req.body);
    });
    app.get(this.path + '/companies', function(req, res) {
        console.log("REST Companies list");
        var responseData = [
            {name: "HAS", persons: [{name: "Hohoh"}]}
        ];
        res.send(responseData);
    });
    app.get(this.path + '/companies/:id', function(req, res) {
        console.log("REST Companies call: " + req.params.id);
        var responseData = {name: "HAS"};
        res.send(responseData);
    });
}

RESTModule.prototype.listen = function() {
    console.log("Starting server at port: " + this.port);
    app.listen(this.port);
};

module.exports = RESTModule;
